//
//  AppDelegate.m
//  QZBaseDemo
//
//  Created by QF_iOS on 2018/11/27.
//  Copyright © 2018年 QF_iOS. All rights reserved.
//

#import "AppDelegate.h"
#import "QZTabbarController.h"
#import "BWGuideViewController.h"
#define kLoadGuide    @"loadGuide"
#import "MBProgressHUD+SGQRCode.h"
#import "UIImageView+WebCache.h"
#import "HttpRequest.h"
#import "MyWebVC.h"
#import "SLWebViewController.h"


@class SLWebViewController;

@interface AppDelegate ()
@property (nonatomic,strong) NSString *isHiddenTabbar;
@property (nonatomic,strong) SLWebViewController *slweb;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    _slweb = [[SLWebViewController alloc] init];
//    COM.barURLString = @"https://www.baidu.com";
    //    UINavigationController * na = [[UINavigationController alloc] initWithRootViewController:mywebvc];
    //    [[UINavigationBar appearance] setHidden:YES];
    self.window.rootViewController = _slweb;
    [self.window makeKeyAndVisible];
    
    //如果使用webview加载
    //获取菜单方法
//    [self childListAction];
//    [self setRootVC];
    //加载引导页和欢迎界面
    [self addLaunchAnimation];
//    [self loadHtmlAction];
    
    
//    [[UINavigationBar appearance] setHidden:YES];
//    [[UITabBar appearance] setHidden:YES];
    return YES;
    //获取导航栏配置
//    [self appNavSet];
    //或者tabbar配置
//    [[UINavigationBar appearance] setHidden:YES];
    
    //导航栏背景图并且去掉NavigationBar下面的横线
//    [[UINavigationBar appearance]  setShadowImage:[[UIImage alloc] init]];
//    [UINavigationBar appearance].translucent = NO;
//    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0.84 green:0.23 blue:0.24 alpha:1.00]];
    
//    5.设置标题的颜色：引入方法
//     [self.navigationBar
//
//         setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:17.0 weight:UIFontWeightLight]}];
    
}



- (void)childListAction {

    NSDictionary *dict = @{@"logoId":LOGOID};
    NSString *urlString = [NSString stringWithFormat:@"%@%@",uHostURL,getAppMenu];
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:urlString parameters:dict success:^(id obj) {
        NSDictionary *dic = (NSDictionary *)obj;
        NSString *errorcodeString = dic[@"errorcode"];
        if ([dic[@"data"] isEqual:[NSNull null]]) {
            return;
        }
        DLog(@"%@",obj);
        if (![errorcodeString isEqualToString:@"9999"]) {
            return;
        }
       
        [Common sharedInstance].childArray = dic[@"data"][@"childList"];
        QZTabbarController *qztab = [[QZTabbarController alloc] init];
        self.window.rootViewController = qztab;
        [qztab addChildViewControllers];
        [self addLaunchAnimation];
//        [self setRootVC];
        
        //0 是显示   1 是隐藏
        NSString *isopenString = dic[@"data"][@"isOpen"];
        if ([isopenString isEqualToString:@"0"]) {
            [[UITabBar appearance] setHidden:YES];
            NSLog(@"走了隐藏tabbar");
            return;
        }
        
//        [[UITabBar appearance] setBarTintColor:[UIColor redColor]];//这样写才能达到效果。
//        [UITabBar appearance].translucent = NO;// 这句表示取消tabBar的透明效果。
        
    } fail:^(NSError *error) {
        
    }];
}


#pragma mark - 添加启动动画
- (void)welcomeImage:(NSString *)str {
    if (str.length == 0) {
        return;
    }
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor whiteColor];
    UIWindow *mainWindow = [UIApplication sharedApplication].keyWindow;
    [mainWindow addSubview:vc.view];
    mainWindow.backgroundColor = [UIColor whiteColor];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window bringSubviewToFront:vc.view];
    //添加广告图
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
//    imageV.backgroundColor = [UIColor clearColor];
    NSString *welImageStr = [NSString stringWithFormat:@"%@%@",uHostURL_IMAGE,str];
    [imageV sd_setImageWithURL:[NSURL URLWithString:welImageStr]];
    [vc.view addSubview:imageV];
    
    [UIView animateWithDuration:0.6f delay:2 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        
        vc.view.alpha = 0.0f;
        vc.view.layer.transform = CATransform3DScale(CATransform3DIdentity, 2.0f, 2.0f, 1.0f);
        
    } completion:^(BOOL finished) {
        [vc.view removeFromSuperview];
//        //判断第一次加载引导页
//        BOOL isFirstLoad = [[NSUserDefaults standardUserDefaults] boolForKey:kLoadGuide];
//        NSLog(@"isFirstLoad = %d", isFirstLoad);
//        if (!isFirstLoad) {
//            NSLog(@"走了引导页了");
//            self.window.rootViewController = [[BWGuideViewController alloc] init];
//            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kLoadGuide];
//            [[NSUserDefaults standardUserDefaults] synchronize];
//        }
    }];
}

- (void)addLaunchAnimation
{
    NSDictionary *dict = @{@"logoId":LOGOID};
       
    NSString *urlString = [NSString stringWithFormat:@"%@%@",uHostURL,getAppWelcome];
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:urlString parameters:dict success:^(id obj) {
        DLog(@"%@",obj);
        if ([obj[@"data"] isEqual:[NSNull null]]) {
//            self->_slweb = [[SLWebViewController alloc] init];
//            UIWindow *app = (UIWindow *) [UIApplication sharedApplication].delegate;
//            app.window.rootViewController = self->_slweb;
//            [self welcomeImage:@""];
            return;
        }
        NSString *welPic = obj[@"data"][@"welPic"];
        [self welcomeImage:welPic];
        
    } fail:^(NSError *error) {
        
    }];
    
    
    //判断第一次加载引导页
    BOOL isFirstLoad = [[NSUserDefaults standardUserDefaults] boolForKey:kLoadGuide];
    NSLog(@"isFirstLoad = %d", isFirstLoad);
    if (!isFirstLoad) {
        NSLog(@"走了引导页了");
        self.window.rootViewController = [[BWGuideViewController alloc] init];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kLoadGuide];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}
    
- (void)setRootVC
{
    //有tabbar的情况下
    QZTabbarController *qztab = [[QZTabbarController alloc] init];
    self.window.rootViewController = qztab;
    [qztab addChildViewControllers];
    [self addLaunchAnimation];
    
//    BOOL isFirstLoad = [[NSUserDefaults standardUserDefaults] boolForKey:kLoadGuide];
//    NSLog(@"isFirstLoad = %d", isFirstLoad);
//    if (!isFirstLoad) {
//        NSLog(@"走了引导页了");
//        self.window.rootViewController = [[BWGuideViewController alloc] init];
//        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kLoadGuide];
//        [[NSUserDefaults standardUserDefaults] synchronize];
//    } else {
//        QZTabbarController *qztab = [[QZTabbarController alloc] init];
//        self.window.rootViewController = qztab;
//        [qztab addChildViewControllers];
//        [self addLaunchAnimation];
//    }
    
    
}


- (void)applicationWillResignActive:(UIApplication *)application {
    NSLog(@"applicationWillResignActive");
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"applicationDidEnterBackground");
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"applicationWillEnterForeground");
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
     NSLog(@"applicationDidBecomeActive");
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
      NSLog(@"applicationWillTerminate");
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
